/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var threeSum = function(nums: number[]) {
    var xs: number[][] = new Array();
    nums.sort(function(x, y) { return x - y; });
    for(var i = 0; i < nums.length; i++) 
    {
        var j = i + 1;
        var k = nums.length - 1;
        while(j < k) {
            if (nums[i] + nums[j] + nums[k] < 0) {
                j++;
            }
            else if(nums[i] + nums[j] + nums[k] > 0) {
                k--;
            }
            else {
                var triple: number[] = [ nums[i], nums[j], nums[k]];
                triple.sort(function(x, y) { return x - y; });
                var existing = 
                    xs.findIndex(function(x) { 
                        return x[0] === triple[0] && x[1] === triple[1] && x[2] === triple[2]; 
                    });
                if (existing === -1)
                {
                    xs.push(triple);
                }
                j++;
                k--;
            }
        }
    }

    return xs;    
};

var s = [-1, 0, 1, 2, -1, -4];
var result = threeSum(s);

console.log("The end");